/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidItemVip;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  查询vip列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidItemVipListPlugin implements PluginConnector {
	private IDao bidItemVipDao;
	private Logger logger = Logger.getLogger(BidItemVipListPlugin.class);

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();

		try {
			result.setResult("true");
			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}

			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by create_time desc ");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidItemVipDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidItemVipDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行BidItemVipList查询失败 ", e);
			result.setMessage("BidItemVipList查询失败！");
		}

		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByProperty", "", "", BidItemVip.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidItemVipAll"));//获取所有活动列表
		if (Validator.isNull(all)) {
			getBidItemVipParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织BidItemVip信息查询条件
	private void getBidItemVipParams(MethodParam param, ParamsVo vo) {
		BidItemVip bidItemVip = new BidItemVip();
		bidItemVip = (BidItemVip) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidItemVip);
		if (Validator.isNotNull(bidItemVip.getItemId())) {
			param.setParams("itemId", bidItemVip.getItemId());
		}
		if (Validator.isNotNull(bidItemVip.getUserEname())) {
			param.setParams("userEname", bidItemVip.getUserEname());
		}
		if (Validator.isNotNull(bidItemVip.getUserCname())) {
			param.setParams("userCname", bidItemVip.getUserCname());
		}
		if (Validator.isNotNull(bidItemVip.getVipType())) {
			param.setParams("vipType", bidItemVip.getVipType());
		}
		if (Validator.isNotNull(bidItemVip.getVipLevel())) {
			param.setParams("vipLevel", bidItemVip.getVipLevel());
		}
		if (Validator.isNotNull(bidItemVip.getVipStatus())) {
			param.setParams("vipStatus", bidItemVip.getVipStatus());
		}
		if (Validator.isNotNull(bidItemVip.getCreateUserEname())) {
			param.setParams("createUserEname", bidItemVip.getCreateUserEname());
		}
		if (Validator.isNotNull(bidItemVip.getCreateIp())) {
			param.setParams("createIp", bidItemVip.getCreateIp());
		}
		if (Validator.isNotNull(bidItemVip.getCreateTime())) {
			param.setParams("createTime", bidItemVip.getCreateTime());
		}
		if (Validator.isNotNull(bidItemVip.getModidyTime())) {
			param.setParams("modidyTime", bidItemVip.getModidyTime());
		}
		if (Validator.isNotNull(bidItemVip.getSpare1())) {
			param.setParams("spare1", bidItemVip.getSpare1());
		}
		if (Validator.isNotNull(bidItemVip.getSpare2())) {
			param.setParams("spare2", bidItemVip.getSpare2());
		}
	}

	public void setBidItemVipDao(IDao bidItemVipDao) {
		this.bidItemVipDao = bidItemVipDao;
	}

}
